package com.wowangz.cms.dailyoperation.store.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wowangz.cms.common.utils.SysConstant;
import com.wowangz.cms.dailyoperation.store.pojo.AdminUser;
import com.wowangz.cms.dailyoperation.store.pojo.StoreEntity;
import com.wowangz.cms.dailyoperation.store.pojo.StoreRedisInfo;
import com.wowangz.cms.syscore.exception.ServiceException;
import com.wowangz.cms.syscore.service.BaseService;
import com.wowangz.cms.systemmanage.city.pojo.City;
import com.wowangz.cms.systemmanage.province.pojo.Province;

/**
 * 商户持久化层
 * 
 * @author cakohy
 *
 */
public class StoreService extends BaseService {

	/**
	 * 保存
	 * 
	 * @param store
	 */
	public void insert(StoreEntity store) throws ServiceException {
		getSqlSession().insert("storeEntity.insert", store);
	}

	/**
	 * 保存初始化的商户用户
	 * 
	 * @param store
	 */
	public void insert(AdminUser user) throws ServiceException {
		getSqlSession().insert("storeEntity.insertUser", user);
	}

	/**
	 * 更新AdminUser的密码
	 * 
	 * @param user
	 * @throws ServiceException
	 */
	public void updateAdminUser(AdminUser user) throws ServiceException {
		getSqlSession().insert("storeEntity.updateAdminUser", user);
	}

	/**
	 * 
	 * @param paramsMap
	 * @return
	 * @throws ServiceException
	 */
	public List<AdminUser> queryAdminUserList(Long storeId) throws ServiceException {
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("storeid", storeId);
		paramsMap.put("statusid", SysConstant.RecordStatus.VALID);
		return this.getSqlSession().selectList("storeEntity.queryAdminUserList", paramsMap);
	}

	/**
	 * 修改
	 * 
	 * @param store
	 */
	public void update(StoreEntity store) throws ServiceException {
		try {
			getSqlSession().insert("storeEntity.update", store);
		} catch (ServiceException e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage(), new Throwable());
		}
	}

	/**
	 * 分页查询总条数
	 * 
	 * @param map
	 * @return
	 */
	public long findResultCount(Map<String, Object> map) throws ServiceException {
		try {
			List<Object> list = getSqlSession().selectList("storeEntity.queryStoreCount", map);
			return list.size();
		} catch (ServiceException e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage(), new Throwable());
		}
	}

	/**
	 * 查询分页的记录数目
	 * 
	 * @param map
	 * @return
	 */
	public List<StoreEntity> findResultList(Map<String, Object> map) {
		try {
			List<StoreEntity> selectList = getSqlSession().selectList("storeEntity.queryPageByParams", map);
			return selectList;
		} catch (ServiceException e) {
			e.printStackTrace();
			throw new ServiceException("查询商户集合异常", new Throwable());
		}
	}

	/**
	 * 获得最大序号
	 * 
	 * @return
	 */
	public Integer queryMaxNumber() {
		Object object = getSqlSession().selectOne("storeEntity.queryMaxNumber");
		Integer num = 0;
		if (object == null) {
			num = 1;
		} else {
			num = (Integer) object + 1;
		}
		return num;
	}

	/**
	 * 查询一个对象
	 * 
	 * @param id
	 * @return
	 * @throws ServiceException
	 */
	public StoreEntity findOne(Long id) throws ServiceException {
		Object object = getSqlSession().selectOne("storeEntity.findOne", id);
		return (StoreEntity) object;
	}

	/**
	 * 审核
	 * 
	 * @param map
	 * @throws ServiceException
	 */
	public void updateApprove(Map<String, Object> map) throws ServiceException {
		getSqlSession().update("storeEntity.updateApprove", map);
	}

	/**
	 * 逻辑删除
	 * 
	 * @param map
	 * @throws ServiceException
	 */
	public void delete(Map<String, Object> map) throws ServiceException {
		getSqlSession().update("storeEntity.updateApprove", map);
	}

	/**
	 * 查询所有的省市
	 * 
	 * @return
	 */
	public List<Province> selectProvinceList() throws ServiceException {

		return getSqlSession().selectList("storeEntity.selectProvinceList");
	}

	/**
	 * 
	 * @author HUANGYUAN
	 * @时间 2015年8月3日 下午3:57:30
	 * @param provinceid
	 * @return 获得城市的列表
	 * @throws ServiceException
	 * @描述
	 */
	public List<City> selectCityByProvinceId(Long provinceid) throws ServiceException {
		return getSqlSession().selectList("storeEntity.selectCityByProvinceId", provinceid);
	}

	/**
	 * @author HUANGYUAN
	 * @时间 2015年8月3日 下午3:57:46
	 * @param map
	 * @return
	 * @描述 获得城市名称的Map集合
	 */
	public Map<String, Object> getCityName(Map<String, Object> map) {
		return getSqlSession().selectOne("storeEntity.getCityName", map);
	}

	/**
	 * @author HUANGYUAN
	 * @时间 2015年8月3日 下午3:58:17
	 * @param map
	 * @return
	 * @描述 获得缓存中的数据集合
	 */
	public List<StoreRedisInfo> findStoreRedisInfo(Map<String, Object> map) {
		return getSqlSession().selectList("storeEntity.findStoreRedisInfo", map);
	}

	public Long findStoreRedisCount(Map<String, Object> map) throws ServiceException {
		return getSqlSession().selectOne("storeEntity.findStoreRedisCount", map);
	}

	/**
	 * @author HUANGYUAN
	 * @时间 2015年8月3日 下午3:59:34
	 * @param map
	 * @return Long
	 * @描述 查询得到记录条数
	 */
	public Long queryStoreInfoNumber(Map<String, Object> map) throws ServiceException {
		return getSqlSession().selectOne("storeEntity.queryStoreInfoNumber", map);
	}

	/**
	 * @author HUANGYUAN
	 * @时间 2015年8月3日 下午4:08:36
	 * @param map
	 * @return
	 * @throws ServiceException
	 * @描述 根据id查询商户的信息
	 */
	public StoreEntity queryStoreById(Map<String, Object> map) throws ServiceException {
		return this.getSqlSession().selectOne("storeEntity.queryStoreById", map);
	}

	/**
	 * 根据网址查询商户是否存在
	 * 
	 * @param webSite
	 * @return
	 * @throws ServiceException
	 */
	public long queryStoreByWebSite(String webSite) throws ServiceException {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		try {
			paramMap.put("statusid", SysConstant.RecordStatus.VALID);
			paramMap.put("website", webSite);
			return getSqlSession().selectOne("storeEntity.queryStoreByWebSite", paramMap);
		} catch (ServiceException e) {
			throw new ServiceException(e.getMessage());
		}
	}
}
